home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 21 / emacsrc / word.c < prev   
Encoding:
C/C++ Source or Header  |  1986-05-14  |  9.1 KB  |  281 lines

  1. /*
  2.  * The routines in this file implement commands that work word at a time.
  3.  * There are all sorts of word mode commands. If I do any sentence and/or
  4.  * paragraph mode commands, they are likely to be put in this file.
  5.  */
  6.  
  7.  
  8. #include        "ed.h"
  9.  
  10.  
  11. /* Word wrap on n-spaces. Back-over whatever precedes the point on the current
  12.  * line and stop on the first word-break or the beginning of the line. If we
  13.  * reach the beginning of the line, jump back to the end of the word and start
  14.  * a new line.  Otherwise, break the line at the word-break, eat it, and jump
  15.  * back to the end of the word.
  16.  *      NOTE:  This function may leaving trailing blanks.
  17.  * Returns TRUE on success, FALSE on errors.
  18.  */
  19. wrapword(n)
  20. int n;
  21. {
  22. /*    bug!    too lazy to fix now and don't like the fix
  23.     that someone posted on the net
  24.     ----
  25.         register int cnt;
  26.     register LINE * oldp;
  27.  
  28.         oldp = curwp->w_dotp;
  29.         cnt = -1;
  30.         do {                            
  31.                 cnt++;
  32.                 if (! backchar(NULL, 1))
  33.                         return(FALSE);
  34.         }
  35.         while (! inword());
  36.         if (! backword(NULL, 1))
  37.                 return(FALSE);
  38.         if (oldp == (int) (curwp->w_dotp && curwp->w_doto)) {
  39.                 if (! backdel(NULL, 1))
  40.                         return(FALSE);
  41.                 if (! newline(NULL, 1))
  42.                         return(FALSE);
  43.         }
  44.         return(forwword(NULL, 1) && forwchar(NULL, cnt));
  45. */
  46. }
  47.                                 
  48. /*
  49.  * Move the cursor backward by "n" words. All of the details of motion are
  50.  * performed by the "backchar" and "forwchar" routines. Error if you try to
  51.  * move beyond the buffers.
  52.  */
  53. backword(f, n)
  54. {
  55.         if (n < 0)
  56.                 return (forwword(f, -n));
  57.         if (backchar(FALSE, 1) == FALSE)
  58.                 return (FALSE);
  59.         while (n--) {
  60.                 while (inword() == FALSE) {
  61.                         if (backchar(FALSE, 1) == FALSE)
  62.                                 return (FALSE);
  63.                 }
  64.                 while (inword() != FALSE) {
  65.                         if (backchar(FALSE, 1) == FALSE)
  66.                                 return (FALSE);
  67.                 }
  68.         }
  69.         return (forwchar(FALSE, 1));
  70. }
  71.  
  72. /*
  73.  * Move the cursor forward by the specified number of words. All of the motion
  74.  * is done by "forwchar". Error if you try and move beyond the buffer's end.
  75.  */
  76. forwword(f, n)
  77. {
  78.         if (n < 0)
  79.                 return (backword(f, -n));
  80.         while (n--) {
  81.                 while (inword() == FALSE) {
  82.                         if (forwchar(FALSE, 1) == FALSE)
  83.                                 return (FALSE);
  84.                 }
  85.                 while (inword() != FALSE) {
  86.                         if (forwchar(FALSE, 1) == FALSE)
  87.                                 return (FALSE);
  88.                 }
  89.         }
  90.         return (TRUE);
  91. }
  92.  
  93. /*
  94.  * Move the cursor forward by the specified number of words. As you move,
  95.  * convert any characters to upper case. Error if you try and move beyond the
  96.  * end of the buffer. Bound to "M-U".
  97.  */
  98. upperword(f, n)
  99. {
  100.         register int    c;
  101.  
  102.         if (n < 0)
  103.                 return (FALSE);
  104.         while (n--) {
  105.                 while (inword() == FALSE) {
  106.                         if (forwchar(FALSE, 1) == FALSE)
  107.                                 return (FALSE);
  108.                 }
  109.                 while (inword() != FALSE) {
  110.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  111.                         if (c>='a' && c<='z') {
  112.                                 c -= 'a'-'A';
  113.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  114.                                 lchange(WFHARD);
  115.                         }
  116.                         if (forwchar(FALSE, 1) == FALSE)
  117.                                 return (FALSE);
  118.                 }
  119.         }
  120.         return (TRUE);
  121. }
  122.  
  123. /*
  124.  * Move the cursor forward by the specified number of words. As you move
  125.  * convert characters to lower case. Error if you try and move over the end of
  126.  * the buffer. Bound to "M-L".
  127.  */
  128. lowerword(f, n)
  129. {
  130.         register int    c;
  131.  
  132.         if (n < 0)
  133.                 return (FALSE);
  134.         while (n--) {
  135.                 while (inword() == FALSE) {
  136.                         if (forwchar(FALSE, 1) == FALSE)
  137.                                 return (FALSE);
  138.                 }
  139.                 while (inword() != FALSE) {
  140.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  141.                         if (c>='A' && c<='Z') {
  142.                                 c += 'a'-'A';
  143.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  144.                                 lchange(WFHARD);
  145.                         }
  146.                         if (forwchar(FALSE, 1) == FALSE)
  147.                                 return (FALSE);
  148.                 }
  149.         }
  150.         return (TRUE);
  151. }
  152.  
  153. /*
  154.  * Move the cursor forward by the specified number of words. As you move
  155.  * convert the first character of the word to upper case, and subsequent
  156.  * characters to lower case. Error if you try and move past the end of the
  157.  * buffer. Bound to "M-C".
  158.  */
  159. capword(f, n)
  160. {
  161.         register int    c;
  162.  
  163.         if (n < 0)
  164.                 return (FALSE);
  165.         while (n--) {
  166.                 while (inword() == FALSE) {
  167.                         if (forwchar(FALSE, 1) == FALSE)
  168.                                 return (FALSE);
  169.                 }
  170.                 if (inword() != FALSE) {
  171.                         c = lgetc(curwp->w_dotp, curwp->w_doto);
  172.                         if (c>='a' && c<='z') {
  173.                                 c -= 'a'-'A';
  174.                                 lputc(curwp->w_dotp, curwp->w_doto, c);
  175.                                 lchange(WFHARD);
  176.                         }
  177.                         if (forwchar(FALSE, 1) == FALSE)
  178.                                 return (FALSE);
  179.                         while (inword() != FALSE) {
  180.                                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  181.                                 if (c>='A' && c<='Z') {
  182.                                         c += 'a'-'A';
  183.                                         lputc(curwp->w_dotp, curwp->w_doto, c);
  184.                                         lchange(WFHARD);
  185.                                 }
  186.                                 if (forwchar(FALSE, 1) == FALSE)
  187.                                         return (FALSE);
  188.                         }
  189.                 }
  190.         }
  191.         return (TRUE);
  192. }
  193.  
  194. /*
  195.  * Kill forward by "n" words. Remember the location of dot. Move forward by
  196.  * the right number of words. Put dot back where it was and issue the kill
  197.  * command for the right number of characters. Bound to "M-D".
  198.  */
  199. delfword(f, n)
  200. {
  201.         register int    size;
  202.         register LINE   *dotp;
  203.         register int    doto;
  204.  
  205.         if (n < 0)
  206.                 return (FALSE);
  207.         dotp = curwp->w_dotp;
  208.         doto = curwp->w_doto;
  209.         size = 0;
  210.         while (n--) {
  211.                 while (inword() == FALSE) {
  212.                         if (forwchar(FALSE, 1) == FALSE)
  213.                                 return (FALSE);
  214.                         ++size;
  215.                 }
  216.                 while (inword() != FALSE) {
  217.                         if (forwchar(FALSE, 1) == FALSE)
  218.                                 return (FALSE);
  219.                         ++size;
  220.                 }
  221.         }
  222.         curwp->w_dotp = dotp;
  223.         curwp->w_doto = doto;
  224.         return (ldelete(size, TRUE));
  225. }
  226.  
  227. /*
  228.  * Kill backwards by "n" words. Move backwards by the desired number of words,
  229.  * counting the characters. When dot is finally moved to its resting place,
  230.  * fire off the kill command. Bound to "M-Rubout" and to "M-Backspace".
  231.  */
  232. delbword(f, n)
  233. {
  234.         register int    size;
  235.  
  236.         if (n < 0)
  237.                 return (FALSE);
  238.         if (backchar(FALSE, 1) == FALSE)
  239.                 return (FALSE);
  240.         size = 0;
  241.         while (n--) {
  242.                 while (inword() == FALSE) {
  243.                         if (backchar(FALSE, 1) == FALSE)
  244.                                 return (FALSE);
  245.                         ++size;
  246.                 }
  247.                 while (inword() != FALSE) {
  248.                         if (backchar(FALSE, 1) == FALSE)
  249.                                 return (FALSE);
  250.                         ++size;
  251.                 }
  252.         }
  253.         if (forwchar(FALSE, 1) == FALSE)
  254.                 return (FALSE);
  255.         return (ldelete(size, TRUE));
  256. }
  257.  
  258. /*
  259.  * Return TRUE if the character at dot is a character that is considered to be
  260.  * part of a word. The word character list is hard coded. Should be setable.
  261.  */
  262. inword()
  263. {
  264.         register int    c;
  265.  
  266.         if (curwp->w_doto == llength(curwp->w_dotp))
  267.                 return (FALSE);
  268.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  269.         if (c>='a' && c<='z')
  270.                 return (TRUE);
  271.         if (c>='A' && c<='Z')
  272.                 return (TRUE);
  273.         if (c>='0' && c<='9')
  274.                 return (TRUE);
  275.         if (c=='$' || c=='_')                   /* For identifiers      */
  276.                 return (TRUE);
  277.         return (FALSE);
  278. }
  279.  
  280. /* -eof- */
  281.